package com.ihdf.serve.minimum_fee.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.ihdf.serve.minimum_fee.entity.MinimumFee;
import com.ihdf.serve.minimum_fee.mapper.SearchMinimumFeeRouteMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/cloud/api/inspect")
@Slf4j
public class MinimumFeeController {
    @Autowired
    SearchMinimumFeeRouteMapper searchMinimumFeeRouteMapper;

    @PostMapping("/getMinimumFeeRoute")
    public String getMinimumFeeRoute(byte[] binFile, String filename, HttpServletRequest request, HttpServletResponse response) {
        log.info("Start getting minimumroute");
        MinimumFee minimumFee;
        //Mf_With_Splitprovince mf_with_splitprovince;
        try {
            String requestJson = new String(binFile,"UTF-8");
            JSONObject jsonObject = JSONObject.parseObject(requestJson);
            String enStationId = (String)jsonObject.get("enStationId");
            String exStationId = (String)jsonObject.get("exStationId");
            Integer vehicleType = (Integer) jsonObject.get("vehicleType");
            if (jsonObject.get("version") == null) {
                //mf_with_splitprovince = get_MF_AND_Splitprovince(enStationId,exStationId,vehicleType);
                minimumFee = get_MF_AND_Splitprovince(enStationId,exStationId,vehicleType);
            } else {
                Long version = (Long) jsonObject.get("version");
                minimumFee = get_MF_AND_Splitprovince_with_version(enStationId,exStationId,vehicleType,version);
                //mf_with_splitprovince = get_MF_AND_Splitprovince_with_version(enStationId,exStationId,vehicleType,version);
            }
            response.setStatus(200);
            if (minimumFee != null) {
                return JSON.toJSONString(minimumFee);

            } else {
                return "Version Not Found";
            }
        }catch (NumberFormatException | JSONException e1) {
            log.error("MinimumFeeInfoError",e1);
            response.setStatus(400);
            return "Data Format Fail";
        } catch (Exception e) {
            log.error("MinimumFeeInfoError",e);
            response.setStatus(500);
            return "System exception";
        }
    }

    private MinimumFee get_MF_AND_Splitprovince_with_version(String enStationId, String exStationId, Integer vehicleType,Long version) {
        MinimumFee minimumFee = searchMinimumFeeRouteMapper.getMinimumFee_with_version(enStationId,exStationId,vehicleType,version);
        minimumFee.setSplitProvinces(searchMinimumFeeRouteMapper.getSplitprovince_with_version(enStationId,exStationId,vehicleType,version));
        return minimumFee;
    }

    private MinimumFee get_MF_AND_Splitprovince(String enStationId, String exStationId, Integer vehicleType) {
        MinimumFee minimumFee = searchMinimumFeeRouteMapper.getMinimumFee(enStationId,exStationId,vehicleType);
        minimumFee.setSplitProvinces(searchMinimumFeeRouteMapper.getSplitprovince(enStationId,exStationId,vehicleType));
        return minimumFee;
    }
}
